<template>
    <div class="container">
        <div class="leftMenu">
            <div v-for="item in memuList" :key="item.name" class='menuItem'
                :class="activeMenu === item.name ? 'activeMenu' : ''" @click="scrollToMenu(item)"
                :v-if="item.children?.length > 0">
                <SvgIcon :svg="item.icon" :size="20" />
                <div class="titleName">{{ item.name }}</div>
            </div>
        </div>
        <div class="chart">
            <div class="imageBigBox">
                <!-- 为每个菜单项的图表区域添加锚点id -->
                <!-- 渲染所有图片 -->
                <div v-for="item in memuList" :key="'images-' + item.name" :id="'images-' + item.name"
                    class="menuImagesSection">
                    <div :id="'anchor-' + item.name" class="menuSection" v-if="item.children?.length > 0">
                        <h3 class="sectionTitle">{{ item.name }}</h3>
                    </div>

                    <div v-for="chartItem in item.children" :key="chartItem.name" class='menuImageItem'>
                        <div v-for="img in chartItem.mainResults" :key="img" class="imageWrapper"
                            @click="showXiangqing(chartItem)">
                            <img :src="showResult_images(img)" alt="" />
                        </div>
                        <div class="chartName">{{ chartItem.name }}</div>
                    </div>
                    <div v-if="item.overflow" class='menuImageItem'>
                        <div class="imageWrapper">
                            <div class="showMore" @click="showMore(item.name)">
                                显示更多…
                            </div>
                        </div>
                        <div class="chartName">&emsp;</div>
                    </div>
                    <!-- 占位元素，用于布局 -->
                    <div class="menuImageItem zhanwei" v-for="i in 9"></div>
                </div>
            </div>
        </div>

        <!-- 弹窗 -->
        <el-dialog v-model="dialogVisible" title="详情" width="1000" align-center :close-on-click-modal="false">
            <div class="dialogContent">
                <div class="titleBox">
                    <span>{{ details.name }}</span>
                    <div class="goToDocument" @click="goToDocument">教程文档</div>
                </div>
                <XlsxViewer v-for="item in details.dataParameters" :key="item" :file-path="item"></XlsxViewer>
                <MdViewer v-for="item in details.code" :key="item" :file-path="item"></MdViewer>
                <div class="mainResult" v-if="details.mainResults">
                    <div class="title">主要结果</div>
                    <img class="mainResultImg" v-for="item in details.mainResults" :key="item"
                        :src="showResult_images(item)" alt="">
                </div>
                <div class="mainResult" v-if="details.Parameters">
                    <div class="title">主要参数</div>
                    <img v-for="item in details.Parameters" :key="item" :src="showResult_images(item)" alt="">
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import { BASE_URL } from '@/utils/zongIp.js'// 静态资源IP地址
import chartsData from '@/utils/chartsData.js'// 脚本编译的文件路径

const activeMenu = ref('柱状图')// 当前激活的菜单
const dialogVisible = ref(false)// 弹窗显示
const memuList = ref([])

// 切换菜单（现在改为滚动到对应位置）
const scrollToMenu = (item) => {
    activeMenu.value = item.name
    // 滚动到对应图片区域
    const element = document.getElementById('images-' + item.name)
    if (element) {
        element.scrollIntoView({
            behavior: 'smooth',
            block: 'start'
        })
    }
}

// menu列表
// details数据
const details = ref({})

// 显示详情
const showXiangqing = (item) => {
    dialogVisible.value = true
    details.value = item
    console.log(item)
}

const showResult_images = (url) => {
    // 判断是否是生产环境
    const isProduction = process.env.NODE_ENV === 'production';
    // 如果是生产环境，则使用BASE_URL
    let baseUrl = isProduction ? BASE_URL : '';
    return baseUrl + url;
}

// 展示pdf
const goToDocument = () => {
    // 获取自己的url
    const currentUrl = window.location.href.split('#')[0];
    // 构建正确的PDF路径
    let pdfPath = `${currentUrl}${details.value.pdf}`;
    // 在新窗口打开PDF
    window.open(pdfPath, '_blank');
}
// 显示更多
const showMore = (name) => {
    // 找到对应的原始数据（包含所有子项目的数据）
    const originalItem = chartsData.find(item => item.name === name);

    if (originalItem && originalItem.children) {
        // 更新memuList中对应的项目，显示所有子项目并移除overflow标记
        const index = memuList.value.findIndex(menuItem => menuItem.name === name);
        if (index !== -1) {
            memuList.value[index] = {
                ...originalItem,
                // 确保显示所有子项目
                children: originalItem.children,
                // 移除overflow标记
                overflow: false
            };
        }

        // 滚动到对应的菜单区域
        scrollToMenu({ name: name });

        // 可选：显示提示信息
        // ElMessage.success(`已显示${name}的全部内容`);
    }
}
onMounted(() => {
    let data = JSON.parse(JSON.stringify(chartsData)); // 深拷贝，避免改源数据
    data.forEach(item => {
        console.log(item);
        if (item.children && item.children.length > 12) {
            let newChildren = [];
            for (let i = 0; i < 11; i++) {
                newChildren.push(item.children[i]);
            }
            memuList.value.push({
                name: item.name,
                icon: item.icon,
                children: newChildren,
                overflow: true
            })
        } else {
            memuList.value.push(item)
        }
    });
    console.log(memuList.value);

    // memuList.value = data;
    activeMenu.value = memuList.value[0]?.name || '';
});
</script>

<style lang="scss" scoped>
.container {
    width: 100%;
    min-width: 1000px;
    margin: auto;
    height: 100%;
    display: flex;

    .leftMenu {
        width: 200px;
        height: 100%;
        background-color: #f0f0f0;
        overflow-y: auto;

        .menuItem {
            padding: 10px;
            height: 20px;
            color: #5067A2;
            display: flex;
            align-items: center;
            cursor: pointer;

            .titleName {
                margin-left: 8px;
                line-height: 20px;
            }
        }

        .activeMenu {
            background-color: #5470c6;
            color: #fff;
        }
    }

    .chart {
        flex: 1;
        font-size: 13px;
        padding:0 10px;
        overflow-y: auto;

        .imageBigBox {
            display: block; // 改为块级元素，垂直排列
            width: 100%;
            max-height: calc(100% - 1px);

            .menuSection {
                width: 100%;

                .sectionTitle {
                    // background-color: #f5f5f5;
                    border-bottom: 1px solid #d8d8d8;
                    padding: 8px 12px;
                    margin: 10px 0 5px 0;
                    // border-left: 4px solid #5470c6;
                    font-size: 17px;
                    font-weight: bold;
                    color: #333;
                }
            }

            .menuImagesSection {
                display: flex;
                flex-wrap: wrap;
                width: 100%;

                // 图表区域样式
                .menuImageItem {
                    text-align: center;
                    width: 210px;
                    margin: 10px auto;
                    display: inline-block; // 横向排列
                    vertical-align: top;

                    .imageWrapper {
                        background-color: #fff;
                        margin-bottom: 8px;
                        border-radius: 4px;
                        height: 100%;
                        overflow: hidden;
                        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
                        transition: transform 0.2s, box-shadow 0.2s;
                        display: flex;
                        align-items: center;

                        &:hover {
                            transform: translateY(-2px);
                            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
                        }

                        img {
                            max-width: 100%;
                            max-height: 100%;
                            object-fit: cover;
                            cursor: pointer;
                        }

                    }
                    .showMore {
                        margin-bottom: 8px;
                        border-radius: 4px;
                        height: 100%;
                        text-align: center;
                        width: 100%;
                        cursor: pointer;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        font-weight: 700;
                        font-size: 20px;
                        color: #666;
                    }

                    .chartName {
                        font-size: 12px;
                        color: #666;
                        word-break: break-word;
                        padding: 0 4px;
                    }
                }

                // // menuImageItem只显示24个,大于的24的隐藏
                // .menuImageItem:nth-child(n+25) {
                //     display: none;
                // }

                // 超过2000像素
                @media (min-width: 2000px) {
                    .menuImageItem {
                        width: calc(95% / 12); // 约 10%，即 1/10
                        // 高度需要等于宽度
                        aspect-ratio: 1 / 1;

                        // img {
                        //     width: 100%;
                        //     aspect-ratio: 1 / 1;
                        // }
                    }
                }

                // 情况一：屏幕宽度在 1000px ~ 2000px 之间
                // 媒体查询
                @media (min-width: 1200px) and (max-width: 2000px) {
                    .menuImageItem {
                        width: calc(90% / 6); // 约 16.666%，即 1/6
                        // 高度需要等于宽度
                        aspect-ratio: 1 / 1;

                        // img {
                        //     width: 100%;
                        //     aspect-ratio: 1 / 1;
                        // }
                    }
                }

                // 情况二：屏幕宽度在 500px ~ 1000px 之间
                @media (min-width: 500px) and (max-width: 1200px) {
                    .menuImageItem {
                        width: 24%; // 占父容器的 1/4
                        aspect-ratio: 1 / 1;

                        // img {
                        //     width: 100%;
                        //     height: auto; // 保持比例，或设置固定高度
                        // }
                    }
                }

                .zhanwei {
                    height: 0;
                    margin: 0 auto;
                }
            }
        }
    }

    .dialogContent {
        max-height: calc(100vh - 200px);
        overflow-y: auto;

        .titleBox {
            display: flex;
            justify-content: space-between;
            font-size: 16px;
            margin: 10px;
            margin-top: 0;

            .goToDocument {
                color: #133d98;
                font-weight: 700;
                border-bottom: 2px solid #133d98;
                margin-bottom: -2px;
                cursor: pointer;
            }
        }

        .mainResult {
            text-align: center;
            width: calc(100% - 20px);

            .title {
                text-align: left;
                font-size: 16px;
                font-weight: bold;
                padding: 10px;
                border-bottom: 1px solid #ccc;
            }

            .mainResultImg {
                height: 400px;
            }

            img {
                max-width: 100%;
            }
        }
    }
}
</style>